<template>
  <el-card class="artical-add-container">
    <div slot="header" class="header">
      <!-- 职位预置项表单-->
      <el-form
        ref="article"
        :model="article"
        label-width="80px"
        style="width: 20%"
      >
        <el-form-item label="职位标题:">
          <el-input
            v-model.trim="article.title"
            placeholder="请填写职位标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="工作地址:">
          <el-input
            v-model.trim="article.address"
            placeholder="请填写工作地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="薪资范围:">
          <el-input
            v-model.trim="article.money"
            placeholder="请填写薪资范围"
          ></el-input>
        </el-form-item>
        <el-form-item label="职位归类:">
          <el-select
            v-model.trim="article.categoryId"
            placeholder="请选择职位类别"
          >
            <el-option
              v-for="item in categorys"
              :key="item.id"
              :label="item.category_name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="职位描述:">
          <el-input
            v-model.trim="article.desprition"
            placeholder="请填写职位描述"
          ></el-input>
        </el-form-item>

        <el-form-item label="公司名称:">
          <el-input
            v-model.trim="article.conpanyName"
            placeholder="请填写公司名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否推荐:" prop="delivery">
          <el-switch v-model.trim="article.isRecommend"></el-switch>
        </el-form-item>
      </el-form>

      <!-- 职位封面上传 -->
      <el-upload
        class="uploader"
        name="coverPicture"
        action="http://localhost:3000/upload/coverPicture/"
        :show-file-list="true"
        :limit="1"
        :on-exceed="handleImageExeed"
        :on-success="handleImageSuccess"
        :before-upload="beforeImageUpload"
      >
        <img
          v-if="imageUrl"
          :src="imageUrl"
          class="picture"
          style="width: 100%"
        />
        <i v-else class="uploader-tip">点击添加公司图片 </i>
      </el-upload>
    </div>
    <div class="button">
      <el-button type="primary" @click="handleAdd">发布</el-button>
    </div>
    <div class="main">
      <!-- 富文本编辑器-双向数据绑定 -->
      <quill-editor
        :options="editorOption"
        :content="article.content"
        @change="onEditorChange($event)"
        style="width: 100%"
      >
      </quill-editor>
    </div>
  </el-card>
</template>

<script>
export default {
  name: "ArticleAdd",
  data() {
    return {
      article: {
        title: "",
        categoryId: "",
        isRecommend: false,
        coverPicture: "",
        content: "",
        address: "",
        money: "",
      },
      imageUrl: "",
      categorys: [],
      editorOption: {
        // quill模块配置项,可以修改和扩展富文本编辑器的功能
      },
    };
  },

  methods: {
    // 富文本输入改变处理函数
    onEditorChange({ quill, html, text }) {
      this.article.content = html;
    },
    // 封面图片上传处理函数
    handleImageSuccess(res, file) {
      this.article.coverPicture = res.result.path;
      this.imageUrl = res.result.path;
    },
    handleImageExeed(files, fileList) {
      if (files.length === 1) {
        this.$message.error("只能上传一张图片");
      }
    },
    beforeImageUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (["image/jpeg", "image/jpg", "image/png"].indexOf(file.type) === -1) {
        this.$message.error("上传头像图片只能是 JPG , PNG , JPEG 格式!");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 5MB!");
        return false;
      }
    },
    handleAdd() {
      let newArticle = {
        title: this.article.title,
        cover_picture: this.article.coverPicture,
        address: this.article.address,
        money: this.article.money,
        content: this.article.content,
        category_id: this.article.categoryId,
        is_recommend: this.article.isRecommend ? 1 : 0,
        desprition: this.article.desprition,
        conpanyName: this.article.conpanyName,
      };
      //给提示框后确认发布职位
      this.$confirm("此操作将确认发布该篇职位, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.post("/articles", newArticle).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "发布成功!",
              });
              this.$router.push("/home/article");
            } else {
              // console.log(res)
              this.$message({
                type: "error",
                message: "输入内容为空,发布失败!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消发布",
          });
        });
    },
  },
  created() {
    // 获取全部分类
    this.$http.get("/categorys").then((res) => {
      if (res.code == 0) {
        this.categorys = res.result;
      } else {
        // console.log(res.msg)
      }
    });
  },
};
</script>

<style lang="stylus">
// 引入主题配色
@import 'styles/theme.styl';

.artical-add-container {
  // height:100%
  // border: 1px solid $black
  .header {
    display: flex;
    justify-content: space-between;

    .el-upload {
      width: 200px;
      height: 180px;
      border: 1px solid #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;

      .uploader-tip {
        font-size: 16px;
        color: #ccc9db;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
      }

      .picture {
        width: 178px;
        height: 178px;
        display: block;
      }
    }
  }

  .main {
    display: flex;
    justify-content: space-between;

    .quill-editor {
      margin-bottom: 40px;
    }
  }

  .button {
    display: flex;
    justify-content: left;
    margin-bottom: 20px;
  }
}
</style>
<style>
textarea {
  min-height: 180px;
}
</style>
